(function() {
  app.controller('topCtrl',function($state,$scope, $rootScope, quoteApi, value_require, header_m, $interval, local_m) {
    var state = $scope.storage.session('top_params');
    var sort = state.sort;
    var params;
    var req = value_require[state.type];
    local_m.set('type_yiyuan',false);
    if (state.type === 'bankuai2') {
      $scope.sort_actived = req.show[0];
      params = angular.extend({ board_id: sort }, req.public_params);
    } else {
      $scope.sort_actived = req.show[sort];
      params = angular.extend(req.private_params[sort], req.public_params);
    }
    if (state.type === 'bankuai') {
      $scope.gp_name = sort === 0 ? '概念名称' : (sort === 2 ? '地区名称' : '行业名称');
    }
    if (state.type === 'zhishu') {
      $scope.gp_name = '指数名称';
    }
    header_m.set({ title_c: state.title ? state.title : req.title[sort] });
    $scope.hangqing_list = [];
    var d = new quoteApi.QuoteApi[req.funid](
      params
    );
    $scope.init = function() { // 行情数据请求
      quoteApi.query(d).then(function(data) {
        $scope.hangqing_list = angular.isArray(data) ? data : data.data;
      });
    };
    var timer = $interval(function() {
      $scope.init();
    }, 3000);
    $scope.$on('$destroy', function() {
      $interval.cancel(timer);
      timer = null;
    });
    $scope.go_detail = function(item) {
      if (state.type === 'bankuai') {
        $scope.storage.session('top_params', { type: 'bankuai2', sort: item.boardId, title: item.stkinfo.name });
        $state.go('top', null, { reload: true });
        return;
      }
      $scope.storage.local('detail_codeinfo', item.stkinfo);
      if (item.stkinfo.style === 2) {
        $state.go('detail');
      } else if (item.stkinfo.style === 1) {
        $state.go('detail_zs');
      }
    };
    $scope.sort_click = function($event, param) { // 排序函数 param为要排序的字段
      var target = angular.element($event.target);
      if (target.hasClass('sort_up')) {
        target.addClass('sort_down');
        target.removeClass('sort_up');
        $scope.order_key = '-' + param; // 排序
      } else if (target.hasClass('sort_down')) {
        target.removeClass('sort_down');
        target.addClass('sort_up');
        $scope.order_key = param;
      } else {
        target.addClass('sort_up');
        $scope.order_key = param;
      }
      angular.forEach(target.siblings(), function(item) {
        angular.element(item).removeClass('sort_up');
        angular.element(item).removeClass('sort_down');
      }, this);
    };
  });
})();